



//
//  NRankViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/23.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NRankViewController.h"
#import "NRankCell.h"
#import "NScoreModel.h"
@interface NRankViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSArray *titles;

@property (nonatomic,assign)NSInteger currentPage;

@end

@implementation NRankViewController

#define cellIdentifier  @"rank"
#define SectionHeaderHeight 10.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        //
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70.f;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        //        [_tableView registerNib:[UINib nibWithNibName:@"NQiyebanshiCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"NRankCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [ws refresh];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [ws loadMoreData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.currentPage = 0;
    self.dataArray = [NSMutableArray array];
    [self.navigationItem setItemWithTitle:@"积分排名" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
}

- (void)refresh {
    self.currentPage = 1;
    [self loadData];
}
- (void)loadMoreData{
    self.currentPage++;
    [self loadData];
}


///没有更多数据
- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)loadData{

    WeakStament(ws);
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dict = @{@"size":@(10),@"curPage":@(self.currentPage)};
    [NNetworkClient getRequestWithBaseUrl:kScoreRankUrl setParameters:dict success:^(id responseObject) {
        YYLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] integerValue] == 0) {
            
            NSArray *arr = responseObject[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NScoreModel  *model = [NScoreModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];
            [ws endRefresh];
            
            
            ///这个放的位置很重要  加载更多没有数据处理
            if (arr.count==0) {
                [ws noMoreData];
            }
            
        }else{
            [AFToast showText:responseObject[@"msg"]];
            [ws endRefresh];
        }
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        [ws endRefresh];
        
    } delegater:nil];

}

- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    return self.titles.count;
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NScoreModel *model = self.dataArray[indexPath.row];
    NRankCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.nameLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    
    
    cell.model = model;
    return cell;
}


#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


@end
